package com.luo.locationapp.customview;

import com.luo.locationapp.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomEditText extends LinearLayout {
	private int width = 0;  
    private String textLabel = "";  
    private String textHintLabel = "";  
    private int image_id = 0;
    private String edttext_inputtype = "";  
    private int label_width = 0;  
    private EditText editText = null;  
    private boolean isReadOnly = true;  
    private String defaultText = "";  
    private TextView txtView = null;  
    private ImageView img = null;  
    private View baseLine1,baseLine2;
    
	public CustomEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText); 
		textLabel = typedArray.getString(R.styleable.CustomEditText_textLabel);  
        textHintLabel = typedArray.getString(R.styleable.CustomEditText_textHintLabel);  
        image_id = typedArray.getResourceId(R.styleable.CustomEditText_hintImage, R.drawable.default_img);  
        edttext_inputtype  = typedArray.getString(R.styleable.CustomEditText_inputType);  
        label_width = Math.round(typedArray.getDimension(R.styleable.CustomEditText_labelWidth, 60));  
        String txtReadOnly = typedArray.getString(R.styleable.CustomEditText_txtReadOnly);  
        if(txtReadOnly!=null&&txtReadOnly.equals("true")){ 
            isReadOnly = false;  
        }  
        typedArray.recycle();
		this.init(context);
	}
	
	private void init(Context context){
		LayoutInflater inflater = LayoutInflater.from(context);
		final View view = inflater.inflate(R.layout.custom_edt_layout, null);
		//this.setOrientation(LinearLayout.HORIZONTAL);
		//this.setLayoutParams(new LinearLayout.LayoutParams(width,LayoutParams.WRAP_CONTENT));
		txtView = (TextView)view.findViewById(R.id.custom_edt_layout_label);
		editText = (EditText)view.findViewById(R.id.custom_edt_layout_txt);
		img = (ImageView) view.findViewById(R.id.custom_edt_layout_img);
		baseLine1 = view.findViewById(R.id.custom_edt_layout_focusline1);
		baseLine2 = view.findViewById(R.id.custom_edt_layout_focusline2);
		
		txtView.setText(textLabel);
		if(label_width != 0){
			txtView.setWidth(label_width);
		}
		editText.setHint(textHintLabel);

		int inputType = getType();
		if(inputType!=-999){
			editText.setInputType(inputType);
		}
		if(defaultText!=null&&defaultText.trim().length()>0){
			editText.setText(defaultText);
		}
		editText.setEnabled(isReadOnly);
		//editText.setEnabled(enabled);
		
		if(image_id != R.drawable.default_img){
			img.setVisibility(View.VISIBLE);
			img.setImageResource(image_id);
		}
		
		editText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				baseLine1.setBackgroundColor(arg1 ? 0xff0099cc:0xffbbbbbb);
				baseLine2.setBackgroundColor(arg1 ? 0xff0099cc:0xffbbbbbb);
			}
		});
		this.addView(view,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
	}
	
	private int getType(){  
        if(edttext_inputtype!=null && edttext_inputtype.trim().length()>0){  
            if(edttext_inputtype.equals("textPassword")){  
                return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;  
            }else if(edttext_inputtype.equals("textPhonetic")){  
                return InputType.TYPE_CLASS_PHONE | InputType.TYPE_TEXT_VARIATION_PHONETIC;  
            }else if(edttext_inputtype.equals("textEmailAddress")){  
                return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;  
            }  
        }
        //return InputType.TYPE_CLASS_TEXT | InputType.TYPE_NULL;  
        return -999;
    } 

	public void setFocus(){
		editText.setFocusable(true);
		editText.setFocusableInTouchMode(true);
		editText.requestFocus();
	}
	public void setText(String str){
		editText.setText(str);
	}
	public String getText(){
		return editText.getText().toString();
	}
}
